<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [后台文件上传]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Filesystem;
use think\facade\Request;
class Upfiles extends Base
{
    /**
     * @return mixed
     * 系统设置
     */
    public function upload(){
        // 获取上传文件表单字段名
        $fileKey = array_keys(request()->file());
        // 获取表单上传文件
        $file = request()->file($fileKey['0']);
        try {
            validate(['image'=>'filesize:10240|fileExt:jpg,png,gif,jpeg'])
                ->check(object2array($file));
            $savename = Filesystem::disk('public')->putFile( 'uploads/system', $file);
            $result['code'] = 0;
            $result['info'] = '图片上传成功!';
            $path=str_replace('\\','/',$savename);
            $hex = checkHex(public_path().$path);
            if($hex==1){
                return ['code'=>0,'msg'=>'请上传合法图片','url'=>''];
            }
            $result['url'] = '/'. $path;
            $result['aid'] = Db::name('attachment')->insertGetId(['url'=>$result['url'],'title'=>input('title'),'addtime'=>time()]);
            return $result;
        } catch (ValidateException $e) {
            $result['code'] =1;
            $result['info'] = $e->getMessage();
            $result['url'] = '';
            return $result;
        }
    }
    /**
     * 后台：栏目缩略图、内容缩略图、单图、多图、广告、
     *  @return mixed
     */
    public function upimage(){
        // 获取上传文件表单字段名
        $fileKey = array_keys(request()->file());
        // 获取表单上传文件
        $file = request()->file($fileKey['0']);
        try {
            validate(['image'=>'filesize:'.config('upload.imageMaxSize').'|fileExt:jpg,png,gif,jpeg'])
                ->check(object2array($file));
            $savename = Filesystem::disk('public')->putFile( 'uploads', $file);
            $result['code'] = 0;
            $result['info'] = '图片上传成功!';
            $path=str_replace('\\','/',$savename);
            $hex = checkHex(public_path().$path);
            if($hex==1){
                return ['code'=>0,'msg'=>'请上传合法图片','url'=>''];
            }
            $result['url'] = '/'. $path;
            $result['aid'] = Db::name('attachment')->insertGetId(['url'=>$result['url'],'title'=>input('title'),'addtime'=>time()]);
            return $result;
        } catch (ValidateException $e) {
            $result['code'] =1;
            $result['info'] = $e->getMessage();
            $result['url'] = '';
            return $result;
        }
    }


    //文件上传
    public function file(){
        // 获取上传文件表单字段名
        $fileKey = array_keys(request()->file());
        // 获取表单上传文件
        $file = request()->file($fileKey['0']);
        try {
            validate(['image'=>'filesize:10240|fileExt:doc,docx,xls,xlsx,ppt,txt,zip,rar'])
                ->check(object2array($file));
            $savename = Filesystem::disk('public')->putFile( 'uploads', $file);
            $result['code'] = 0;
            $result['info'] = '文件上传成功!';
            $path=str_replace('\\','/',$savename);
            $result['url'] = '/'. $path;
            return $result;
        } catch (ValidateException $e) {
            $result['code'] =1;
            $result['info'] = $e->getMessage();
            $result['url'] = '';
            return $result;
        }
    }


    public function uploaderMediaNewsImg()
    {
        // 获取上传文件表单字段名
        $fileKey = array_keys(request()->file());
        // 获取表单上传文件
        $file = request()->file($fileKey['0']);
        try {
            validate(['image'=>'filesize:10240|fileExt:jpg,png,gif,jpeg'])->check(object2array($file));
            $savename = Filesystem::disk('public')->putFile( 'uploads', $file);
            $result['code'] = 1;
            $result['message'] = '图片上传成功!';
            $path=str_replace('\\','/',$savename);
            $hex = checkHex(public_path().$path);
            if($hex==1){
                return ['code'=>0,'msg'=>'请上传合法图片','url'=>''];
            }
            $result['data'] = Request::domain().'/'. $path;
            return json($result);
        } catch (ValidateException $e) {
            $result['code'] =0;
            $result['message'] = $e->getMessage();
            $result['data'] = '';
            return json($result);
        }
    }

    public function uploadImg(){
        // 获取上传文件表单字段名
        $fileKey = array_keys(request()->file());
        // 获取表单上传文件
        $file = request()->file($fileKey['0']);
        try {
            validate(['image'=>'filesize:10240|fileExt:jpg,png,gif,jpeg'])->check(object2array($file));
            $savename = Filesystem::disk('public')->putFile( 'uploads', $file);
            $path=str_replace('\\','/',$savename);
            $path = Request::domain().'/'. $path;
            $hex = checkHex(public_path().$path);
            if($hex==1){
                return ['code'=>0,'msg'=>'请上传合法图片','url'=>''];
            }
            return ['code' => 0, 'data' => ['src' => $path]];
        } catch (ValidateException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    public function uploadMedia()
    {
        // 获取上传文件表单字段名
        $fileKey = array_keys(request()->file());
        // 获取表单上传文件
        $file = request()->file($fileKey['0']);
        try {
            //validate(['image'=>'filesize:10240|fileExt:mp3,wma,wav,amr,rm,rmvb,wmv,avi,mpg,mpeg,mp4'])->check(object2array($file));
            validate(['image'=>'filesize:10240|fileExt:fileExt:mp3,wma,wav,amr,rm,rmvb,wmv,avi,mpg,mpeg,mp4'])->check(object2array($file));

            $savename = Filesystem::disk('public')->putFile( 'uploads', $file);
            $path=str_replace('\\','/',$savename);
            $hex = checkHex(public_path().$path);
            if($hex==1){
                return ['code'=>0,'msg'=>'请上传合法文件','url'=>''];
            }
            $path = Request::domain().'/'. $path;
            return ['code' => 0, 'data' => ['src' => $path]];
        } catch (ValidateException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 后台：NKeditor
     * @return \think\response\Json
     */
    public function editimg(){
        // 获取上传文件表单字段名
        $fileKey = array_keys(request()->file());
        // 获取表单上传文件
        $file = request()->file($fileKey['0']);
        $allowExtesions = array(
            'image' => 'filesize:10240|fileExt:jpg,png,gif,jpeg',
            'flash' => 'fileExt:swf,flv',
            'media' => 'fileExt:swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb',
            'file' => 'fileExt:doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2',
        );
        try {
            validate($allowExtesions)->check(object2array($file));
            $savename = Filesystem::disk('public')->putFile( 'uploads', $file);
            $result['code'] = '000';
            $result['message'] = '上传成功!';
            $path=str_replace('\\','/',$savename);
            $hex = checkHex(public_path().$path);
            if($hex==1){
                return ['code'=>0,'message'=>'请上传合法文件','url'=>''];
            }
            $result['data']['url'] = '/'. $path;
            $data['url'] = $result['data']['url'];
            $data['type'] = input('fileType');
            $data['add_time'] = time();
            Db::name('editor_file')->insert($data);
            return json_encode($result);
        } catch (ValidateException $e) {
            $result['code'] =0;
            $result['message'] = $e->getMessage();
            $result['data']['url'] = '';
            return $result;
        }
    }

    public function getFile(){
        $page =input('page')?input('page'):1;
        $page = ['list_rows'=> '15', 'page' => $page];
        $fileType=input('fileType');
        $list = Db::name('editor_file')->where('type',$fileType)
            ->field('*,url as thumbURL,url as oriURL')
            ->order('add_time desc')
            ->paginate($page);
        $list = $list->toArray();
        return ['code'=>000,'message'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
    }

}